require 'net/http'
require 'rubygems'
require 'json'

class PownceUser
  
  attr_accessor :app_key
  attr_accessor :age
  attr_accessor :username
  attr_accessor :password
  attr_accessor :friend_count
  attr_accessor :permalink
  attr_accessor :fan_count
  attr_accessor :short_name
  attr_accessor :country
  attr_accessor :max_upload_mb
  attr_accessor :first_name
  attr_accessor :profile_photo_urls
  attr_accessor :gender
  attr_accessor :blurb
  attr_accessor :fan_of_count
  attr_accessor :id
  attr_accessor :pro
  attr_accessor :location

  # returns XML for now as the profile...sorry
  def profile()
    Net::HTTP.start('api.pownce.com') { |http|
      req = Net::HTTP::Get.new("/2.0/users/" + @username + ".json?app_key=" + @app_key)
      response = http.request(req)
      return response.body
    }
  end
  
  # returns an array of PownceUser objects
  def friends()
    peers('friends')
  end
  
  # returns an array of PownceUser objects
  def fans()
    peers('fans')
  end
  
  private
  
    def peers(type)
      peers = Array.new
      Net::HTTP.start('api.pownce.com') { |http|
        req = Net::HTTP::Get.new('/2.0/users/'+ @username + '/' + type + '.json?app_key=' + @app_key)
        response = http.request(req)
        jsonObj = JSON.parse(response.body)
        jsonObj[type]['users'].each do |user|
          p = PownceUser.new
          p.username = user['username']
          p.permalink = user['permalink']
          p.short_name = user['short_name']
          p.country = user['country']
          p.age = user['age']
          p.max_upload_mb = user['max_upload_mb']
          p.first_name = user['first_name']
          p.blurb = user['blurb']
          p.gender = user['gender']
          p.id = user['id']
          p.pro = user['is_pro']
          p.location = user['location']
          peers << p
        end
        return peers
      }
      
    end
  

end